/*
 *  Copyright (C) 2010 Matthias Buch-Kromann <mbk.isv@cbs.dk>
 * 
 *  This file is part of the MatrixParser package.
 *  
 *  The MatrixParser program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 * 
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.osdtsystem.incparser.features;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Matthias Buch-Kromann <mbk.isv@cbs.dk>
 */
public abstract class AbstractFeatureVector implements FeatureVector {
    @Override
    public AbstractFeatureVector clone() {
        try {
            return (AbstractFeatureVector) super.clone();
        } catch (CloneNotSupportedException ex) {
            throw new RuntimeException("Should support cloning, but doesn't", ex);
        }
    }

    public void writeToFile(String outputFile) {
        Writer writer = null;
        try {
            writer = new PrintWriter(outputFile, "UTF-8");
            writeToFile(writer);
            writer.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(FeatureHandlerMapBased.class.getName()).log(Level.SEVERE, null, ex);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(FeatureHandlerMapBased.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(FeatureHandlerMapBased.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public abstract void writeToFile(Writer writer) throws IOException;

    @Override
    public String toString() {
        Writer writer = new StringWriter();
        try {
            writeToFile(writer);
        } catch (IOException ex) {
            Logger.getLogger(AbstractFeatureVector.class.getName()).log(Level.SEVERE, null, ex);
        }
        return writer.toString();
    }
}
